package com.yuanfeng.commoms.util;

import com.alibaba.fastjson.JSON;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.constant.CommonType;
import com.yuanfeng.commoms.dto.userms.UserInfoDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.exception.DefinedException;
import com.yuanfeng.commoms.exception.GlobalExceptionEnum;
import com.yuanfeng.commoms.vo.user.UserInfoVO;

/**
 * @author Administrator
 */
public class ParseTokenUtils {

    /**
     * 根据token得到userInfo
     *
     * @param token
     * @return
     */
    public static UserInfoVO getUserInfoByToken(String token) {
        //从redis得到token转换userInfo
        boolean isExist = RedisUtil.exists(YFConfig.YF_USER_TOKEN_USER + token);
        if (!isExist) {
            throw new DefinedException(GlobalExceptionEnum.NO_FORBIDDEN.getErrorCode(), BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }else{
            String userInfoStr = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
            UserInfoDTO dto = JSON.parseObject(userInfoStr, UserInfoDTO.class);
            if (dto != null) {
                return dto.getUser();
            } else {
                return null;
            }
        }

    }
    /**
     * 根据token得到userInfo
     * token 不存在 也返回
     * @param token
     * @return
     */
    public static UserInfoVO getUserInfoByTokenNoIsExist(String token) {
        //从redis得到token转换userInfo
        boolean isExist = RedisUtil.exists(YFConfig.YF_USER_TOKEN_USER + token);
        if (!isExist) {
            return null;
        }else{
            String userInfoStr = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
            UserInfoDTO dto = JSON.parseObject(userInfoStr, UserInfoDTO.class);
            if (dto != null) {
                return dto.getUser();
            } else {
                return null;
            }
        }

    }
    /**
     * 得到系统用户信息
     *
     * @param token
     * @return
     */
    public static ResponseResult getPlatformUserInfo(String token) {
        String userInfoStr = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
        if (null == userInfoStr || "".equals(userInfoStr)) {
            return ResponseResult.fail(BizCodeEnume.QUERY_USER_FAIL.getCode(), BizCodeEnume.QUERY_USER_FAIL.getMsg());
        }
        // 判断是否是系统管理员
        UserInfoVO ui = getUserInfoByToken(token);
        if (null == ui || !CommonType.loginPlatform.BOSS_LOGIN.getCode().equals(ui.getLoginPlatform())) {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        return ResponseResult.success(ui);
    }

    /**
     * 得到商家用户信息
     *
     * @param token
     * @return
     */
    public static ResponseResult getShopUserInfo(String token) {
        String userInfoStr = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
        if (null == userInfoStr || "".equals(userInfoStr)) {
            return ResponseResult.fail(BizCodeEnume.QUERY_USER_FAIL.getCode(), BizCodeEnume.QUERY_USER_FAIL.getMsg());
        }
        // 判断是否是商家账号
        UserInfoVO ui = getUserInfoByToken(token);
        if (null == ui || !CommonType.commonStatus.YES.getCode().equals(ui.getIsShop())&&ui.getShopId()==null) {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS.getCode(), BizCodeEnume.INSUFFICIENT_PERMISSIONS.getMsg());
        }
        String userId = RedisUtil.get(YFConfig.YF_USER_TOKEN_USERID + token);
        return ResponseResult.success(userId, ui);
    }


    /**
     * 得到系统用户信息
     *
     * @param token
     * @return
     */
    public static UserInfoVO getPlatformUser(String token) {
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + token);
        if (null == userInfoStr || "".equals(userInfoStr)) {
             userInfoStr = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
            if (null == userInfoStr || "".equals(userInfoStr)) {
                throw new DefinedException(GlobalExceptionEnum.NO_FORBIDDEN.getErrorCode(), BizCodeEnume.TOKEN_IS_INVALID.getMsg());
            }
        }
        UserInfoDTO dto = JSON.parseObject(userInfoStr, UserInfoDTO.class);
        if (dto != null) {
            return dto.getUser();
        } else {
            return null;
        }
    }



}
